﻿using System;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Headsight.BPMN2WF.Transformation.BPMN2TransformerMappings.Config;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Headsight.BPMN2WF.Transformation.BPMN2TransformerMappings
{
    [TestClass]
    public class BPMN2WFTransformationsConfigTest
    {
        private readonly string _mappingFile = Properties.Settings.Default.TestingRootDir + @"\Headsight.BPMN2WFTest\Transformation\MappingConfigFiles\MappingAdvanced.xml";
        private const string TransformationType = "Headsight.BPMN2WF.Transformation.Transformers.BPMN2WFTransformationDummyActivity";
        private const string TransformationAssembly = "Headsight.BPMN2WF.Transformation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e236fd441f43256f";

        [TestMethod]
        public void TestLoadAssemblyConst()
        {
            var loadAssembly = Assembly.Load(TransformationAssembly);
            Assert.AreEqual(TransformationAssembly, loadAssembly.FullName);
        }

        [TestMethod]
        public void TestCreateTypeInstanceReferencedAssembliesConst()
        {
            var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == TransformationAssembly);
            Assert.IsNotNull(assembly);
            var instanceType = assembly.GetType(TransformationType);
            Assert.IsNotNull(instanceType);
            var obj = Activator.CreateInstance(instanceType);
            Assert.IsNotNull(obj);
        }

        [TestMethod]
        public void TestCreateTypeInstanceConst()
        {
            var assembly = Assembly.Load(TransformationAssembly);
            var instanceType = assembly.GetType(TransformationType);
            Assert.IsNotNull(instanceType);
            var obj = Activator.CreateInstance(instanceType);
            Assert.IsNotNull(obj);
        }

        [TestMethod]
        public void TestLoadAssemblyXml()
        {
            var xmlDoc = new XmlDocument();
            xmlDoc.Load(_mappingFile);
            var root = xmlDoc.DocumentElement;
            var mapping1 = root.SelectSingleNode("mappings").ChildNodes[0];
            Assert.IsNotNull(AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == mapping1.Attributes["assembly"].Value));
        }

        [TestMethod]
        public void TestCreateTypeInstanceXml()
        {
            var xmlDoc = new XmlDocument();
            xmlDoc.Load(_mappingFile);
            var root = xmlDoc.DocumentElement;
            var mapping1 = root.SelectSingleNode("mappings").ChildNodes[0];
            var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == mapping1.Attributes["assembly"].Value);
            var type = assembly.GetType(mapping1.Attributes["type"].Value);
            Assert.IsNotNull(Activator.CreateInstance(type));
        }

        [TestMethod]
        public void TestReadBPMN2WFTransformationConfigMapDefault()
        {
            var xmlDoc = new XmlDocument();
            xmlDoc.Load(_mappingFile);
            var mapping = BPMN2WFTransformationConfigMaps.ReadConfigFromXml(xmlDoc);
            Assert.AreEqual("Headsight.BPMN2WF.Transformation.Transformers.BPMN2WFTransformationWriteLine", mapping.DefaultDestinationObject.ToString());
        }

        [TestMethod]
        public void TestReadBPMN2WFTransformationConfigMap()
        {
            var xmlDoc = new XmlDocument();
            xmlDoc.Load(_mappingFile);
            var mapping = BPMN2WFTransformationConfigMaps.ReadConfigFromXml(xmlDoc);
            var destObjName = mapping.ConfigMaps[0].DestinationObject.ToString();
            Assert.AreEqual(TransformationType, destObjName);
        }
    }
}
